package com.guoliang.orderService.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.guoliang.commonUtils.CommonResult;
import com.guoliang.commonUtils.JwtUtils;
import com.guoliang.orderService.entity.Order;
import com.guoliang.orderService.service.OrderService;
import com.guoliang.orderService.service.UcenterClient;
import com.guoliang.service.exceptionHandler.MyException;
import com.sun.jmx.snmp.mpm.SnmpMsgTranslator;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 订单 前端控制器
 * </p>
 *
 * @author leo
 * @since 2023-01-31
 */
@RestController
@RequestMapping("/orderService/order")
public class OrderController {
   @Autowired
   private OrderService orderService;

   @ApiOperation("生成订单")
   @PostMapping("createOrder/{courseId}")
    public CommonResult createOrder(@PathVariable("courseId")String courseId, HttpServletRequest request){
      String memberId = JwtUtils.getMemberIdByJwtToken(request);
      if (StringUtils.isEmpty(memberId)) return CommonResult.ok().code(28004).message("请先登录");
      String orderNo=orderService.createOrder(courseId,memberId);
       return CommonResult.ok().data("orderId",orderNo);
   }
   @ApiOperation("根据订单id查询订单信息")
   @GetMapping("getOrderInfo/{id}")
   public CommonResult getOrderInfo(@PathVariable("id") String id){
      QueryWrapper<Order> wrapper=new QueryWrapper<>();
      wrapper.eq("order_no",id);
      Order order = orderService.getOne(wrapper);
      return CommonResult.ok().data("order",order);
   }
   @ApiOperation("根据课程id和用户id查询订单表中订单状态")
    @GetMapping("isBuy/{courseId}/{memberId}")
    public boolean isBuy(@PathVariable String courseId, @PathVariable String memberId){
       QueryWrapper<Order> wrapper=new QueryWrapper<>();
       wrapper.eq("course_id",courseId);
       wrapper.eq("member_id",memberId);
       wrapper.eq("status",1);
       long count = orderService.count(wrapper);
       return count>0?true:false;
   }
}
